package cn.otrue.patienthealthmanager.activity;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPException;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.DoctorModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.DialogUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.xmpp.XmppTool;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 找医生->医生详情
 * 
 * @author G
 * 
 */
public class DoctorParticularsActivity extends BaseActivity {
	private DoctorModel tuuu;

	private String aliasAccount;
	private AlertDialog alertDialog;
	private String zhuyangtai;
	Handler removeHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {
				Toast.makeText(DoctorParticularsActivity.this, "取消关注成功", 0)
						.show();
				iv_doctor_info_attention
						.setImageResource(R.drawable.guanzhuyisheng_on);
				iv_doctor_info_attention.setTag("0");
			} else {
				Toast.makeText(DoctorParticularsActivity.this, "取消关注失败", 0)
						.show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			// TODO Auto-generated method stub
			super.onFail(data, state);
		}
	};

	Handler attentionHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {
				Toast.makeText(DoctorParticularsActivity.this, "关注成功", 0)
						.show();

				iv_doctor_info_attention
						.setImageResource(R.drawable.guanzhuyisheng);
				iv_doctor_info_attention.setTag("1");
			} else {
				Toast.makeText(DoctorParticularsActivity.this, "关注失败", 0)
						.show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			// TODO Auto-generated method stub
			super.onFail(data, state);
		}
	};

	public void ImageSet(String imgurl, ImageView img_headPhoto) {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(
								DoctorParticularsActivity.this, 40))).build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctor_particulars);
		getView();
	}

	private void getView() {
		getDate();
		iv_goumaifuwu = (ImageView) findViewById(R.id.iv_goumaifuwu);
		iv_goumaifuwu.setOnClickListener(this);
		iv_goumaifuwu.setTag("0");

		ll_shabi = (LinearLayout) findViewById(R.id.ll_shabi);
		ll_pinglun = (LinearLayout) findViewById(R.id.ll_pinglun);
		ll_pinglun.setOnClickListener(this);
		pinglunzhu = (ImageView) findViewById(R.id.pinglunzhu);
		tv_pinglunrongnei = (TextView) findViewById(R.id.tv_pinglunrongnei);
		tv_pinglunmingzi = (TextView) findViewById(R.id.tv_pinglunmingzi);
		tv_pinglunshijian = (TextView) findViewById(R.id.tv_pinglunshijian);
		iv_doctor_info_attention = (ImageView) findViewById(R.id.iv_doctor_info_attention);
		iv_doctor_info_attention.setOnClickListener(this);
		fensi = (TextView) findViewById(R.id.fensi);
		haoping = (TextView) findViewById(R.id.haoping);
		huoyue = (TextView) findViewById(R.id.huoyue);
		jianjie = (TextView) findViewById(R.id.jianjie);
		shanchang = (TextView) findViewById(R.id.shanchang);
		tv_mingzi = (TextView) findViewById(R.id.tv_mingzi);
		pe_head_image = (ImageView) findViewById(R.id.pe_head_image);
		rb_evaluation_professional = (RatingBar) findViewById(R.id.rb_evaluation_professional);
		rb_evaluation_professio = (RatingBar) findViewById(R.id.rb_evaluation_professio);
		rb_evaluation_profes = (RatingBar) findViewById(R.id.rb_evaluation_profes);
		if (isAttention != null && (Integer.parseInt(isAttention) > 0)
				&& !TextUtils.isEmpty(MyConst.getACCOUNT())) {
			iv_doctor_info_attention
					.setImageResource(R.drawable.guanzhuyisheng);
			iv_doctor_info_attention.setTag("1");
		} else {
			iv_doctor_info_attention
					.setImageResource(R.drawable.guanzhuyisheng_on);
			iv_doctor_info_attention.setTag("0");
		}

		getidid();

		initTitleBar();
		getdop();
	}

	private void initTitleBar() {

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);

		tv_title_back.setOnClickListener(this);
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.drug));

	}

	private void getDate() {
		aliasAccount = getIntent().getStringExtra("aliasAccount");
		jid = getIntent().getStringExtra("jid");
		isAttention = getIntent().getStringExtra("isAttention");
	}

	private Handler Handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String tuu = data.getString(HttpCons.DATA);
			if (state.equals("001")) {

				Gson gson = HttpServer.gson;
				tuuu = gson.fromJson(tuu, DoctorModel.class);
				if (tuuu.getHeadimg() != null) {
					String img = tuuu.getHeadimg().toString();
					ImageSet(img, pe_head_image);
				}
				zhuyangtai = tuuu.getState();

				if (zhuyangtai.equals("1")
						&& !TextUtils.isEmpty(MyConst.getACCOUNT())) {
					iv_goumaifuwu.setImageResource(R.drawable.goumaizhong);

				} else if (zhuyangtai.equals("3")
						&& !TextUtils.isEmpty(MyConst.getACCOUNT())) {
					iv_goumaifuwu.setImageResource(R.drawable.yigoumai);
				}
				if (tuuu.getProfessional() != null) {
					int zhuanye = tuuu.getProfessional();
					rb_evaluation_professional.setRating(zhuanye);
				}

				if (tuuu.getAttitude() != null) {
					int zhuan = tuuu.getAttitude();
					rb_evaluation_professio.setRating(zhuan);
				}

				if (tuuu.getStar() != null) {
					int zhuane = tuuu.getStar();
					rb_evaluation_profes.setRating(zhuane);
				}

				if (tuuu.getFans() != null) {
					String fans = tuuu.getFans() + "";
					fensi.setText("粉丝:" + fans);
				}

				if (tuuu.getFeedbackrate() != null) {
					String feed = tuuu.getFeedbackrate() + "";
					haoping.setText("好评:" + feed + "%");
				}

				if (tuuu.getActivity() != null) {
					String acti = tuuu.getActivity() + "";
					huoyue.setText("活跃度:" + acti);
				}

				if (tuuu.getIntro() != null) {
					String intro = tuuu.getIntro() + "";
					jianjie.setText(intro);
				}

				if (tuuu.getGood_at_subject() != null) {
					String good = tuuu.getGood_at_subject() + "";
					shanchang.setText(good);
				}
				if (tuuu.getRealname() != null) {
					String go = tuuu.getRealname() + "";
					tv_mingzi.setText("姓名:" + go);
				}

				Toast.makeText(DoctorParticularsActivity.this, "操作成功", 0)
						.show();
			} else {
				Toast.makeText(DoctorParticularsActivity.this, "msg", 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(DoctorParticularsActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private ImageView pe_head_image;
	private TextView fensi;
	private TextView haoping;
	private TextView huoyue;
	private TextView jianjie;
	private TextView shanchang;
	private TextView tv_mingzi;
	private String jid;
	private String isAttention;
	private ImageView iv_doctor_info_attention;

	private void getidid() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("aliasAccount", aliasAccount);
		map.put("patientId", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword(" ");
		parameter.setData(map);
		new HttpServer().post(HttpCons.queryDoctorByid, parameter, Handler);

	}

	Handler dopHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String tuu = data.getString(HttpCons.DATA);
			if (state.equals("001")) {
				List<Map<String, String>> gooo = HttpServer.gson.fromJson(tuu,
						new TypeToken<List<Map<String, String>>>() {
						}.getType());
				if (gooo != null && gooo.size() > 0) {
					ll_shabi.setVisibility(View.VISIBLE);
					if (gooo.get(0).get("headimg") != null) {
						String urL = gooo.get(0).get("headimg");
						ImageSet(urL, pinglunzhu);
					}
					if (gooo.get(0).get("nickName") != null) {
						tv_pinglunmingzi.setText(gooo.get(0).get("nickName"));
					}
					if (gooo.get(0).get("content") != null) {
						tv_pinglunrongnei.setText(gooo.get(0).get("content"));
					}
					if (gooo.get(0).get("createtime") != null) {
						long num = Long
								.parseLong(gooo.get(0).get("createtime"));
						Date uuu = new Date(num);

						String dateStr = CommonUtil.getFormatDate("yyyy-MM-dd",
								uuu);
						tv_pinglunshijian.setText(dateStr);

					}
					Toast.makeText(DoctorParticularsActivity.this, msg, 0)
							.show();
				} else {

					Toast.makeText(DoctorParticularsActivity.this, "暂无数据", 0)
							.show();
				}

			} else {
				Toast.makeText(DoctorParticularsActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			// TODO Auto-generated method stub
			super.onFail(data, state);
		}
	};
	private TextView tv_pinglunshijian;
	private TextView tv_pinglunmingzi;
	private TextView tv_pinglunrongnei;
	private ImageView pinglunzhu;
	private LinearLayout ll_pinglun;
	private LinearLayout ll_shabi;
	private RatingBar rb_evaluation_professional;
	private RatingBar rb_evaluation_professio;
	private RatingBar rb_evaluation_profes;

	private ImageView iv_goumaifuwu;

	private void getdop() {
		HashMap<String, Object> map = new HashMap<String, Object>();

		TransferParameter parameter = new TransferParameter();
		parameter.setUsername(aliasAccount);
		parameter.setPassword("");
		parameter.setData(map);
		new HttpServer()
				.post(HttpCons.newEvaluationInfo, parameter, dopHandler);

	}

	private View initDataInputDialog(DataDialogListener dataListener, int i) {
		LayoutInflater inflater = LayoutInflater
				.from(DoctorParticularsActivity.this);
		View view = inflater.inflate(R.layout.dialog_input_daaa, null);
		TextView tv_catt_dialogLeft = (TextView) view
				.findViewById(R.id.tv_catt_dialogLeft);
		tv_catt_dialogLeft.setOnClickListener(dataListener);
		TextView tv_catt_dialogRight = (TextView) view
				.findViewById(R.id.tv_catt_dialogRight);
		tv_catt_dialogRight.setOnClickListener(dataListener);
		return view;

	}

	public class DataDialogListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.tv_catt_dialogLeft:// 取消
				alertDialog.dismiss();
				break;
			case R.id.tv_catt_dialogRight:// 确定

				Intent personinfoIntent = new Intent(
						DoctorParticularsActivity.this, LoginActivity.class);
				startActivity(personinfoIntent);

			}
		}

	}


	private void uAttentionDoctor() {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("sender", MyConst.getACCOUNT());
		map.put("receiver", aliasAccount);
		TransferParameter parameter = new TransferParameter();
		parameter.setData(map);
		new HttpServer().post(HttpCons.DELETE_DOCTOR_ATTENTION_INFO, parameter,
				removeHandler);

	}

	Handler reHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {

			if (state.equals("001")) {
				Toast.makeText(DoctorParticularsActivity.this, msg, 0).show();
				iv_goumaifuwu.setImageResource(R.drawable.goumaizhong);
				iv_goumaifuwu.setTag("1");
				// 成功后加oppenFile关注

			} else {
				Toast.makeText(DoctorParticularsActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			// TODO Auto-generated method stub
			super.onFail(data, state);
		}
	};

	private void goumaiDoctor() {

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("doctorId", aliasAccount);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername(MyConst.getACCOUNT());
		parameter.setData(map);
		new HttpServer().post(HttpCons.insertDoctorPointService, parameter,
				reHandler);

	}

	private View initDataInputDialogi(DataDialogListeneri dataListener, int i) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.dialog_input_daget, null);
		TextView tv_catt_dialogLeft = (TextView) view
				.findViewById(R.id.tv_catt_dialogLeft);
		tv_catt_dialogLeft.setOnClickListener(dataListener);
		TextView tv_catt_dialogRight = (TextView) view
				.findViewById(R.id.tv_catt_dialogRight);
		tv_catt_dialogRight.setOnClickListener(dataListener);
		return view;

	}

	public class DataDialogListeneri implements OnClickListener {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.tv_catt_dialogLeft:// 取消
				alertDialog.dismiss();
				break;
			case R.id.tv_catt_dialogRight:// 确定
				uAttentionDoctor();
				alertDialog.dismiss();
			}
		}

	}

	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();
			break;
		case R.id.iv_doctor_info_attention:
			if (MyConst.getACCOUNT().equals("")) {

				View dialogView = initDataInputDialog(new DataDialogListener(),
						0);
				if (null != dialogView) {
					alertDialog = DialogUtil.showDialog(this, dialogView, true);
				}
			} else {
				if (arg0.getTag().toString().equals("0")) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("sender", MyConst.getACCOUNT());
					map.put("receiver", aliasAccount);
					TransferParameter parameter = new TransferParameter();
					parameter.setData(map);
					Log.e("XMPP", "INSERT_DOCTOR_ATTENTIONINFO");
					new HttpServer().post(HttpCons.INSERT_DOCTOR_ATTENTIONINFO,
							parameter, attentionHandler);
				} else {

					View dialogView = initDataInputDialogi(
							new DataDialogListeneri(), 0);
					alertDialog = DialogUtil.showDialog(this, dialogView, true);

				}
			}

			break;

		case R.id.iv_goumaifuwu:

			if (MyConst.getACCOUNT().equals("")) {

				View dialogView = initDataInputDialog(new DataDialogListener(),
						0);
				if (null != dialogView) {
					alertDialog = DialogUtil.showDialog(this, dialogView, true);
				}
			} else {
				if (arg0.getTag().toString().equals("0")) {
					goumaiDoctor();
				} else {
					iv_goumaifuwu
							.setOnClickListener(DoctorParticularsActivity.this);
					iv_goumaifuwu.setClickable(true);
				}
			}

			break;
		case R.id.ll_pinglun:
			Intent intent = new Intent(DoctorParticularsActivity.this,
					YishengpinglunActivity.class);
			intent.putExtra("aliasAccount", aliasAccount);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
